const Controller = require('../core/base_controller');

const assert = require('assert')

class AppController extends Controller {

    async page() {
        const { app } = this

        const ret = this.service.project.page(this.param)

        this.success(ret)
    }

    async save() {
        let { project, products, costs } = this.param
        //保存项目
        const { app } = this
        let ret = app.table('project').save(project)
        //保存产品
        assert(products, '产品不能为空！')
        for (var p of products) {
            p.proj_id = ret.id
            app.table('project_product').save(p)
        }
        //保存科目
        assert(costs, '成本科目不能为空！')
        for (var cost of costs) {
            cost.proj_id = ret.id
            app.table('project_cost').save(cost)
        }
        this.success()
    }

    async remove() {
        let { project: { id } } = this.param
        this.service.project.remove(id)
        this.success()
    }
}
module.exports = AppController;
